package com.xiaocg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaocg.entity.User;
import com.xiaocg.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xiaocg
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private IUserService iUserService;

    @GetMapping("/{id}")
    public Object getById(@PathVariable("id") Long id){
        return iUserService.getById(id);
    }

    @GetMapping()
    public Object getAll(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("username", "xiao");
        return iUserService.list(queryWrapper);
    }

    @GetMapping("/{current}/{size}")
    public Object pageAll(@PathVariable("current") Integer current,@PathVariable("size") Integer size,
                        String username){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(username),"username", username);
        Page<User> page = new Page<>(current,size);
        return iUserService.page(page,queryWrapper);
    }
}
